/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.action;

import cn.edu.nju.software.greencreditsystem.domain.Enterprise;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentWarning;
import cn.edu.nju.software.greencreditsystem.domain.FinancialWarning;
import cn.edu.nju.software.greencreditsystem.domain.RepaymentRecord;
import cn.edu.nju.software.greencreditsystem.factory.ServiceFactory;
import cn.edu.nju.software.greencreditsystem.service.EnterpriseService;
import cn.edu.nju.software.greencreditsystem.service.EnvironmentExcelService;
import cn.edu.nju.software.greencreditsystem.service.FinancialExcelService;
import cn.edu.nju.software.greencreditsystem.service.RepaymentRecordService;
import cn.edu.nju.software.greencreditsystem.service.WarningService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 作者：韩天
 * 创建时间：2011-8-13
 * 类描述：企业登录模块的action封装类
 */
public class EnterpriseLoginAction extends ActionSupport {
    private String userName;//用户名
    private String password;//密码
    private Enterprise enterprise;//企业实例
    private int enterpriseId;//企业的id
    private RepaymentRecord record;//下次还贷记录
    private int leftDays;//据下次还款天数
    private int financialLeftDays;//据下次提交财务报表天数
    private int environmentLeftDays;//据下次提交环境报表天数
    private List<FinancialWarning> fwarningList;//财务警告
    private List<EnvironmentWarning> ewarningList;//环境警告
    private List<String> policyList;//最新政策
    
    
    @Override
    /**
     * 方法描述：默认的执行方法，实现无条件跳转
     * param：
     * return：SUCCESS
     */
    public String execute() {
        return SUCCESS;
    }
    
    /**
     * 方法描述：登录
     * param：
     * return：SUCCESS
     */
    public String login() {
        //取得请求相关的ActionContext实例  
        ActionContext actionContext = ActionContext.getContext();
        Map session = actionContext.getSession();
        enterpriseId = Integer.valueOf((String) session.get("userId"));
        
        //取得企业对象
        EnterpriseService us = ServiceFactory.getEnterpriseServiceInstance();
        this.enterprise = us.getEnterpriseById(enterpriseId);
        
        //取得还贷情况概要
        RepaymentRecordService rrs = ServiceFactory.getRepaymentRecordServiceInstance();
        this.record = rrs.getNextUnpaid(enterpriseId);
        
        //计算据下次还款天数
        Date today = new Date(System.currentTimeMillis());
        long mm = 1000;
        this.leftDays = (int)((long)(record.getPlanDate().getTime()-today.getTime())/(mm*60*60*24));
        
        //取得报表提交动态
        EnvironmentExcelService es = ServiceFactory.getEnvironmentExcelServiceInstance();
        FinancialExcelService fs = ServiceFactory.getFinancialExcelServiceInstance();
        
        this.financialLeftDays = fs.getNextLeftDays(enterpriseId);
        this.environmentLeftDays = es.getNextLeftDays(enterpriseId); 
                
       //取得警告
       WarningService ws = ServiceFactory.getWarningServiceInstance();
       this.fwarningList = ws.get_TwoFinancialWarning(enterpriseId);
       this.ewarningList = ws.get_TwoEnvironmentWarning(enterpriseId);
       
       //初始化最新政策
       this.initPolicy();
        
        return SUCCESS;
    }
    
    /**
     * 方法描述：初始化最新政策
     * param：
     * return：SUCCESS
     */
    public void initPolicy(){
        this.policyList = new ArrayList<String>();
        policyList.add("关于收回破产企业睿神集团贷款的通知");
        policyList.add("财务报表模板的二次调整");
        policyList.add("《国家可持续发展实验区管理办法》等文件的通知(转)");
        policyList.add("热烈祝贺'coin'小组获得花旗杯冠军(图)");
    }
    
    

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public int getEnvironmentLeftDays() {
        return environmentLeftDays;
    }

    public void setEnvironmentLeftDays(int environmentLeftDays) {
        this.environmentLeftDays = environmentLeftDays;
    }

    public List<EnvironmentWarning> getEwarningList() {
        return ewarningList;
    }

    public void setEwarningList(List<EnvironmentWarning> ewarningList) {
        this.ewarningList = ewarningList;
    }

    public int getFinancialLeftDays() {
        return financialLeftDays;
    }

    public void setFinancialLeftDays(int financialLeftDays) {
        this.financialLeftDays = financialLeftDays;
    }

    public List<FinancialWarning> getFwarningList() {
        return fwarningList;
    }

    public void setFwarningList(List<FinancialWarning> fwarningList) {
        this.fwarningList = fwarningList;
    }

    public int getLeftDays() {
        return leftDays;
    }

    public void setLeftDays(int leftDays) {
        this.leftDays = leftDays;
    }

    public RepaymentRecord getRecord() {
        return record;
    }

    public void setRecord(RepaymentRecord record) {
        this.record = record;
    }

    public List<String> getPolicyList() {
        return policyList;
    }

    public void setPolicyList(List<String> policyList) {
        this.policyList = policyList;
    }
}
